package com.nit.store.controller;

import com.nit.store.pojo.dto.output.ProductDto;
import com.nit.store.service.ProductService;
import com.nit.store.util.JwtUtils;
import com.nit.store.web.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author J1ay
 * @Date 2022/7/21 20:12
 * @Description
 */
@RestController
@RequestMapping("/products")
@Slf4j
public class ProductController {
    @Autowired
    ProductService productService;

    /**
     * 热销排行榜
     * @return
     */
    @GetMapping("/hot")
    public JsonResult<List<ProductDto>> getHotList() {
        return JsonResult.ok(productService.getHotList());
    }

    @GetMapping("/{id}/details")
    public JsonResult<List<ProductDto>> getProduct(@PathVariable("id") Long id) {
        return JsonResult.ok(productService.getProduct(id));
    }


    @PreAuthorize("hasAuthority('/product/show')")
    @PostMapping("/search")
    public JsonResult<List<ProductDto>> getProductByKey(String key) {
        return JsonResult.ok(productService.getProductByKey(key));
    }
}
